
/**
 * Created by wxnacy on 2015/8/12.
 */
$(function() {
    //初始化上传视频组件
    initSource();
    //生成直播
    createActivity();
    //直播中的不能让用户点击
    //initQueryLive();

});

function initQueryLive(){
    var activityId = $('#activityId').val();
    if(activityId == ''){
        return;
    }

    var url = "/vrs/api/live/query.jsn?activityId="+activityId;
    var html = '';
    $.ajax({
        url: url,
        type: "GET",
        //async: true,        //异步
        //processData: false,  //很重要，告诉jquery不要对form进行处理
        //contentType: false,  //很重要，指定为false才能形成正确的Content-Type
        success: function (data) {
            var status = data.status;
            if(status == 1){
                var activityStatus = data.data.activityStatus;
                if(activityStatus == 1){
                    var $createActivity = $('#createActivity');
                    console.log($createActivity);
                    $createActivity.text('直播中');
                    $createActivity.prop('disabled');
                }
            }
        }
    });

}

function queryLive(activityId){

}


/**
 * 创建或修改直播活动
 */
function createActivity(){
    var $createActivity = $('.createActivity');
    $createActivity.click(function(){
        var $source = $('#sourceInput');
        var activityId = $('#activityId').val();
        var videoName = $('#videoName').val();
        var showTime = $('#showTime').val();
        var endTime = $('#endTime').val();
        var stageName = $('#stageNamesInput').val();

        var $isNeedPushAuth = $('[name="isNeedPushAuth"]');
        var isNeedPushAuth = 0;
        if($isNeedPushAuth.prop('checked')){
            isNeedPushAuth = 1;
        }
        //console.log($isNeedPushAuth.prop('checked'));
        //console.log($isNeedPushAuth.val());
        //$isNeedPushAuth.each(function(){
        //    console.log(this.checked);
        //});
        //return;

        if(videoName == '' || showTime == ''|| endTime == ''|| stageName==''){
            swal({
                title: '请确认舞台，视频名称，开始结束时间已经正确填写',
                text: '点击返回',
                type:'error'
            });
            return;
        }
        showTime = $.stringToTimeStamp(showTime);
        endTime = $.stringToTimeStamp(endTime);
        var stages = stageName.split(',');


        var url = "/vrs/api/live/create.jsn?videoName="+videoName+"&showTime="+showTime+"&endTime="+endTime+"&liveNum="+stages.length+"&activityId="+activityId+"&isNeededPushAuth="+isNeedPushAuth;
        var $table = $('#sourceTable');
        var html = '';
        $.ajax({
            url: url,
            type: "GET",
            async: true,        //异步
            processData: false,  //很重要，告诉jquery不要对form进行处理
            contentType: false,  //很重要，指定为false才能形成正确的Content-Type
            success: function (data) {

                var status = data.status;
                if(status == 1){
                    var activityId = data.data.activityId;
                    var lives = data.data.lives;
                    $('input.activityId').val(activityId);
                    $('div.activityId').text(activityId);
                    var source = '';
                    for(var i=0;i<lives.length;i++){
                        var pushUrl = lives[i].pushUrl;
                        pushUrl = pushUrl.substring(pushUrl.lastIndexOf('/')+1);
                        source = source+pushUrl+',';
                        html = html + '<tr><th>'+stages[i]+'</th><th>'+pushUrl+'</th></tr>';
                    }
                    $table.find('tbody').html(html);
                    $('[data-theme-name="create-live"]').show();//.removeClass('hidden');
                    //将舞台信息放入到隐藏域中
                    if(source != ''){
                        source = source.substring(0,source.lastIndexOf(','));
                        $source.val(source);
                    }
                }
            }
        });
    });
}

/**
 * 初始化直播信息
 */
function initSource(){
    var source = $('#sourceInput').val();

    var stage = $('#stageNamesInput').val();
    if(source == "" || stage == ""){
        return;
    }
    var sources = source.split(',');
    var stages = stage.split(',');
    var $table = $('#sourceTable');
    var html = '';
    for(var i=0;i<sources.length;i++){
        html = html + '<tr><th>'+stages[i]+'</th><th>'+sources[i]+'</th></tr>';
    }
    $table.find('tbody').html(html);
    var $createLive = $('[data-theme-name="create-live"]');
    $createLive.removeClass('hidden');

}


